package beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class PossibleActions {
	
	public static class OptionLink
	{
		private String optionName, linkString;

		public OptionLink(String optionName, String linkString) {
			this.optionName = optionName;
			this.linkString = linkString;
		}

		public OptionLink() {
		}

		public String getOptionName() {
			return optionName;
		}

		public void setOptionName(String optionName) {
			this.optionName = optionName;
		}

		public String getLinkString() {
			return linkString;
		}

		public void setLinkString(String linkString) {
			this.linkString = linkString;
		}
		
		
	}
	
	private String type;
	private ArrayList<OptionLink> actions;
	
	BasicDataSource ds;
	
	public PossibleActions(String type, BasicDataSource ds)
	{
		this.type = type;
		actions = new ArrayList<OptionLink>();
		this.ds = ds;
		loadActions();
	}
	
	public void loadActions()
	{
		Connection cnn = null;
		Statement stat = null;;
		ResultSet rs = null;
		
		try {
			cnn = ds.getConnection();
			stat = cnn.createStatement();
			rs = stat.executeQuery("SELECT * FROM actions WHERE userType = '" + type + "'");
			while(rs.next())
			{
				String act = rs.getString("actionName");
				String link = rs.getString("link");
				actions.add(new OptionLink(act, link));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(cnn != null)
			{
				try {
					cnn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(stat != null)
			{
				try {
					stat.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public ArrayList<OptionLink> getActions() {
		return actions;
	}

	public void setActions(ArrayList<OptionLink> actions) {
		this.actions = actions;
	}
	
}
